import server from "./server";


function myserver() {
  this.server = server;
  this.nowhandle = null;
}

/**
 *
 *
 * @param {*} name 模块名称 login、shop
 * @param {*} urlOb 模块内接口地址对象
 */
myserver.prototype.parseRouter = function (name, urlOb) {

  this[name] = {};

  Object.keys(urlOb).forEach((apiName) => {
    this[name][apiName].state = 'ready';
    this[name][apiName] = this.sendMes.bind(this, name, apiName, urlOb[apiName]);
  });

}

myserver.prototype.v = function (obj) {
  this.nowhandle = obj;
  return this;
}


// 造轮子核心思想，不要写死，留出可扩展接口

// 方法 ==》 留一个参数

// 组件 ==》 留一个props

/**
 *
 *
 * @param {*} moduleName 模块名称
 * @param {*} apiName api名称，模块内对象的key
 * @param {*} url url地址 模块内对象key的值
 */
myserver.prototype.sendMes = function (moduleName, apiName, url, config) {

  var config = config || {}
  // 请求类型处理
  var type = config.type || 'get';
  var data = config.data || {};
  var bindName = config.bindName || apiName;
  var that = this;

  // 模块划分，是可扩展性的基础
  // 请求处理  before
  // loading
  var before = function (msg) {
    that[moduleName][apiName].state = 'ready';
  }

  var defaultFn = function (msg) {
    that.nowhandle[bindName] = mes.data;
  }

  var success = config.success || defaultFn;

  if (this[moduleName][apiName].state == 'ready') {
    this.server[type](url, data).then(before).then(success);
    this[moduleName][apiName].state = 'waiting';
  }



}


export default new myserver();